import { deepClone } from "@/utils";
import { encrypt, decrypt } from "@/utils/auth";
import { systemTunnyRequest } from "@/utils/request";
import { apiActivateUser, apiAddUser, apiDeleteUsers, apiUpdateUser } from "./tunnyApi";

export default class SystemTunnyApi {
  public login(data: any) {
    return systemTunnyRequest({
      url: "/token/get",
      method: "POST",
      data
    });
  }

  public refreshToken(data: any) {
    return systemTunnyRequest({
      url: "/token/refresh",
      method: "POST",
      data
    });
  }

  public async getSystemUserList(params: any) {
    const result: any = await systemTunnyRequest({
      url: "/systemuser",
      method: "GET",
      params: {
        pageSize: params.page_size,
        pageNum: params.page,
        keyWord: params.search
      }
    });

    try {
      result.rows = result.rows.map((user: any) => ({
        id: user.id,
        username: user.accountName,
        real_name: user.realName,
        mobile: user.phone ? decrypt(user.phone) : "",
        role_id: user.roleId,
        role_name: user.roleName,
        email: user.mail ? decrypt(user.mail) : "",
        is_active: !!user.isEnabled,
        description: user.description
      }));
    } catch (error) {
      console.log("failed to get system user", error, result);
    }

    console.log("systemuserList", result);

    return result;
  }

  public async getSystemUserById(id: number) {
    const result = await systemTunnyRequest({
      url: "/systemuser/getDetail",
      method: "GET",
      params: { id }
    });

    console.log("!!!!!!!!", result);
    

    return result;
  }

  public getSystemUserByToken() {
    return systemTunnyRequest({
      url: "/systemuser/getDetailByToken",
      method: "GET"
    });
  }

  public async createSystemUser(_data: any) {
    const data = deepClone(_data);

    data.mobile = data.mobile ? encrypt(data.mobile) : "";
    data.email = data.email ? encrypt(data.email) : "";
    data.password = encrypt(data.password);
    await apiAddUser(data);
    return await systemTunnyRequest({
      url: "/systemuser/addUser",
      method: "POST",
      data: {
        accountName: data.username,
        phone: data.mobile,
        mail: data.email,
        description: data.description,
        isEnabled: 1,
        realName: data.real_name,
        roleId: data.rules,
        password: data.password
      }
    });
  }

  public async changeSystemUserById(id: number, _data: any) {
    const data = deepClone(_data);
    return await systemTunnyRequest({
      url: "/systemuser",
      method: "PUT",
      data: {
        id,
        accountName: data.username,
        // phone: encrypt(data.mobile),
        // mail: encrypt(data.email),
        phone: data.mobile ? encrypt(data.mobile) : "",
        mail: data.email ? encrypt(data.email) : "",
        description: data.description,
        isEnabled: data.is_active ? 1 : 0,
        realName: data.real_name,
        roleId: data.rules || data.role_id,
        password: encrypt(data.password)
      }
    });
  }

  public async changeSystemUserStateByIds(ids: number[], isEnabled: number) {
    return await systemTunnyRequest({
      url: "/systemuser/updateStatus",
      method: "DELETE",
      data: {
        ids,
        isEnabled
      }
    });
  }

  public async deleteSystemUserByIds(rows: any[]) {
    const ids = rows.map((row) => row.id);
    const accountNames = rows.map((row) => row.username);

    return await systemTunnyRequest({
      url: "/systemuser",
      method: "DELETE",
      data: {
        ids,
        accountNames
      }
    });
  }

  public async changeSystemUserStatus(rows: any[], isEnabled: boolean) {
    const ids = rows.map((row) => row.id);

    return await systemTunnyRequest({
      url: "/systemuser/updateStatus",
      method: "DELETE",
      data: {ids, isEnabled: (isEnabled ? 1 : 0) }
    });
  }

  public async checkPassword(password: string) {
    return await systemTunnyRequest({
      url: "/systemuser/checkPassword",
      method: "POST",
      data: {
        password: encrypt(password)
      }
    })
  }

  public async resetLoginCount(rows: any[]) {
    const ids = rows.map((row) => row.id);

    return await systemTunnyRequest({
      url: "/systemuser/resetLoginCount",
      method: "PUT",
      data: { ids }
    });
  }

  public getRulesList(params: any) {
    return systemTunnyRequest({
      url: "/role",
      method: "GET",
      params: {
        pageSize: params.page_size,
        pageNum: params.page,
        keyWord: params.search
      }
    });
  }

  public changeRuleById(data: any) {
    return systemTunnyRequest({
      url: "/role",
      method: "PUT",
      data
    });
  }

  public changeRuleStatus(rows: any[], isEnabled: boolean) {
    const requestList = [];
    for (let i = 0; i < rows.length; i++) {
      const row = deepClone(rows[i]);
      requestList.push(systemTunnyRequest({
        url: "/role",
        method: "PUT",
        data: {
          id: row.id,
          isEnabled: (isEnabled ? 1 : 0)
        }
      }));
    }
    return Promise.all(requestList);
  }

  public getAllRules() {
    return systemTunnyRequest({
      url: "/role/getList",
      method: "GET"
    });
  }

  public getRuleById(id: number) {
    return systemTunnyRequest({
      url: "/role/getDetail",
      method: "GET",
      params: {
        id
      }
    });
  }

  public getRuleMenuList(roleId: any) {
    return systemTunnyRequest({
      url: "/menu",
      method: "GET",
      params: { roleId }
    });
  }

  public getDirector() {
    return systemTunnyRequest({
      url: "/director",
      method: "GET"
    });
  }
  /**
 * 校验密码
 */
  public apiCheckPassword = async (password: any) => {
    return await systemTunnyRequest({
      // url: "/users/self/check_password",
      url: "/systemuser/checkPassword",
      method: "POST",
      data: {
        // password
        password: encrypt(password)
      }
    });
  }
  public apiPutUserSettings = async (data: any) => {
    return await systemTunnyRequest({
      url: "/director",
      method: "PUT",
      data
    });
  }
}